package com.libraries.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.libraries.helper.Logger;
import com.libraries.helper.StringHelper;
import com.libraries.view.listener.IBaseDialogListener;
import com.libraries.view.toast.Toaster;
import com.example.libraries.R;

/**
 * 自定义弹出框
 * 
 * @author pansha
 * 
 */
public class AlertDialog extends Dialog implements OnClickListener {
	private boolean editable;
	private EditText et_alert;
	/**
	 * tv_title 标题 tv_message 提示内容
	 */
	private TextView tv_alertTitle, tv_alertInfo;
	private IBaseDialogListener dialogListener;
	/**
	 * title 标题 message 提示内容 center 按钮Text left 左边统一确定按钮Text right 右边统一取消按钮Text
	 */
	private Object title, alert, center, left, right;

	public AlertDialog(Context context) {
		super(context);
		// this.dialogListener = (IBaseDialogListener) context;
	}

	/**
	 * 实例化自定义弹出框（提示内容）
	 * 
	 * @param context
	 *            实现了IDialogListener接口的Context
	 * @param message
	 *            提示内容
	 */
	public AlertDialog(Context context, Object alert) {
		super(context);
		this.alert = alert;
		// this.dialogListener = (IBaseDialogListener) context;
	}
	
	public AlertDialog(Context context, String title,boolean edit) {
		super(context);
		this.title = title;
		this.editable=edit;
		// this.dialogListener = (IBaseDialogListener) context;
	}

	/**
	 * 实例化自定义弹出框（提示内容、按钮）
	 * 
	 * @param context
	 *            实现了IDialogListener接口的Context
	 * @param message
	 *            提示内容
	 * @param buttonText
	 *            按钮Text
	 */
	public AlertDialog(Context context, Object title, Object buttonText) {
		super(context);
		this.title = title;
		this.center = buttonText;
		// this.dialogListener = (IBaseDialogListener) context;
	}

	/**
	 * 实例化自定义弹出框（提示内容、按钮）
	 * 
	 * @param context
	 *            实现了IDialogListener接口的Context
	 * @param title
	 *            标题
	 * @param positiveButtonText
	 *            确定按钮Text
	 * @param negativeButtonText
	 *            取消按钮Text
	 */
	public AlertDialog(Context context, Object title, Object leftText,
			Object rightText) {
		super(context);
		this.title = title;
		this.left = leftText;
		this.right = rightText;
		// this.dialogListener = (IBaseDialogListener) context;
	}

	/**
	 * 实例化自定义弹出框（提示内容、按钮）
	 * 
	 * @param context
	 *            实现了IDialogListener接口的Context
	 * @param title
	 *            标题
	 * @param positiveButtonText
	 *            确定按钮Text
	 * @param negativeButtonText
	 *            取消按钮Text
	 */
	public AlertDialog(Context context, Object title, Object leftText,
			Object rightText, boolean flag) {
		super(context);
		this.title = title;
		this.left = leftText;
		this.right = rightText;
	}

	/**
	 * 实例化自定义弹出框（提示内容、按钮）
	 * 
	 * @param context
	 * @param title
	 * @param alertInfo
	 * @param positiveButtonText
	 * @param negativeButtonText
	 */
	public AlertDialog(Context context, Object title, Object alert,
			Object leftText, Object rightText) {
		super(context);
		this.title = title;
		this.alert = alert;
		this.left = leftText;
		this.right = rightText;
		// this.dialogListener = (IBaseDialogListener) context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Logger.log(this, "onCreate");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.alertdialog_layout);
		tv_alertTitle = (TextView) findViewById(R.id.tv_alertTitle);
		tv_alertInfo = (TextView) findViewById(R.id.tv_alertInfo);
		if (center != null) {
			Button btn_center = (Button) findViewById(R.id.btn_dcenter);
			findViewById(R.id.ll_dlayout).setVisibility(View.GONE);
			btn_center.setVisibility(View.VISIBLE);

			if (center != null) {
				String scenter = center.toString();
				if (StringHelper.hasValue(scenter)) {
					btn_center.setText(scenter);
				}
				int icenter = Integer.parseInt(center.toString());
				if (icenter >= 100000000) {
					btn_center.setText(icenter);
				}
			}
			btn_center.setOnClickListener(this);
		} else {
			findViewById(R.id.ll_dlayout).setVisibility(View.VISIBLE);
			Button btn_left = (Button) findViewById(R.id.btn_dleft);
			Button btn_right = (Button) findViewById(R.id.btn_dright);
			if (left != null) {
				String sleft = left.toString();
				if (StringHelper.hasValue(sleft)) {
					btn_left.setText(sleft);
				}
				int ileft = Integer.parseInt(left.toString());
				if (ileft >= 100000000) {
					btn_left.setText(ileft);
				}
			}

			if (right != null) {
				String sright = right.toString();
				if (StringHelper.hasValue(sright)) {
					btn_right.setText(sright);
				}
				int iright = Integer.parseInt(right.toString());
				if (iright >= 100000000) {
					btn_right.setText(iright);
				}
			}
			btn_left.setOnClickListener(this);
			btn_right.setOnClickListener(this);
		}
		if (title != null) {
			tv_alertTitle.setVisibility(View.VISIBLE);
			if (title instanceof String) {
				tv_alertTitle.setText(title.toString());
			}
			else if (title instanceof Integer) {
				int ititle = Integer.parseInt(title.toString());
				if (ititle >= 100000000) {
					tv_alertTitle.setText(ititle);
				}
			}
		}
		if (alert != null) {
			String sAlert = alert.toString();
			if (StringHelper.hasValue(sAlert)) {
				tv_alertInfo.setText(sAlert);
			}
			int iAlert = Integer.parseInt(alert.toString());
			if (iAlert > 100000000) {
				tv_alertInfo.setText(iAlert);
			}
			tv_alertInfo.setVisibility(View.VISIBLE);
		}
		
		if (editable) {
			et_alert=(EditText) findViewById(R.id.et_alert);
			et_alert.setVisibility(View.VISIBLE);
		}
		windowDeploy();
	}

	public void setDialogListener(IBaseDialogListener dialogListener) {
		this.dialogListener = dialogListener;
	}

	@SuppressWarnings("deprecation")
	public void windowDeploy() {
		Window window = getWindow(); // 得到对话框
		window.setBackgroundDrawableResource(R.color.transparent);
		WindowManager.LayoutParams wl = window.getAttributes();
		wl.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
		wl.gravity = Gravity.CENTER;
		//wl.dimAmount = 0f;//是否透明背景
		WindowManager m = window.getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		wl.width = d.getWidth();
		wl.height = d.getHeight();
		window.setAttributes(wl);
	}

	@Override
	public void onClick(View v) {
		Logger.log(this, "onClick");
		int id = v.getId();
		if (id == R.id.btn_dleft) {
			if (editable) {
				String inputStr=et_alert.getText().toString();
				if (!StringHelper.hasValue(inputStr)) {
					Toaster.show(getContext(), "请输入文本");
					return;
				}
				dialogListener.onPositiveButtonClicked(v,inputStr);
			}
			else{
				dialogListener.onPositiveButtonClicked(v);
			}
		} else if (id == R.id.btn_dright) {
			dialogListener.onNegativeButtonClicked(v);
		} else if (id == R.id.btn_dcenter) {
			dialogListener.onButtonClicked(v);
		} else {
		}
		dismiss();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Logger.log(this, "onKeyDown");
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	@Override
	public void dismiss() {
		super.dismiss();
		title = null;
		alert = null;
		center = null;
		left = null;
		right = null;
	}

	/**
	 * 设置标题
	 * 
	 * @param title
	 *            标题
	 * @return AlertDialog
	 */
	public AlertDialog setTitle(String title) {
		this.title = title;
		return this;
	}

	/**
	 * 设置提示内容
	 * 
	 * @param message
	 *            提示内容
	 * @return AlertDialog
	 */
	public AlertDialog setMessage(String alert) {
		this.alert = alert;
		return this;
	}

	/**
	 * 设置按钮Text
	 * 
	 * @param text
	 * @return AlertDialog
	 */
	public AlertDialog setButtonText(String text) {
		this.center = text;
		return this;
	}

	/**
	 * 设置确定按钮Text
	 * 
	 * @param text
	 * @return AlertDialog
	 */
	public AlertDialog setPositiveButtonText(String text) {
		this.left = text;
		return this;
	}

	/**
	 * 设置取消按钮Text
	 * 
	 * @param text
	 * @return AlertDialog
	 */
	public AlertDialog setNegativeButtonText(String text) {
		this.right = text;
		return this;
	}

	public void show(IBaseDialogListener dialogListener) {
		this.dialogListener = dialogListener;
		show();
	}

}
